package com.example.tys.baseapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tys.baseapplication.View.recycleView.BaseViewHolder;
import com.example.tys.baseapplication.View.recycleView.TimeLineDividerItemDecoration;
import com.example.tys.baseapplication.View.recycleView.sticky.StickyAdapter;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {

    private RecyclerView rv;
    private Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        rv = findViewById(R.id.rv);


        adapter = new Adapter(this, new ArrayList<>());
        rv.setAdapter(adapter);
        adapter.setItemClickListen((itemView, position) -> {
            Toast.makeText(Main2Activity.this, position + "", Toast.LENGTH_SHORT).show();
        });
        for (int i =0; i<32;i++){
            adapter.addData(""+i);
        }
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new TimeLineDividerItemDecoration(this, adapter));
    }


    class Adapter extends StickyAdapter<String, Adapter.VH> {

        protected Adapter(Context context, ArrayList<String> mData) {
            super(context, mData);
        }

        @Override
        public String getItemViewTitle(int position) {
            if (position > 10) {
                return "4";
            } else if (position > 6) {
                return "3";
            } else if (position > 2) {
                return "2";
            } else if(position > 1){
                return "1";
            }else {
                return "0";
            }
        }


        @Override
        public Adapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
            VH vh = new VH(LayoutInflater.from(context).
                    inflate(R.layout.simple_list_item_1, parent, false));
            return vh;
        }

        @Override
        public void onBindViewHolder(Adapter.VH holder, int position) {
            holder.tv.setText(String.valueOf(position));
        }


        public class VH extends BaseViewHolder {
            private final TextView tv;

            protected VH(View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv);
                itemView.setOnClickListener(v -> {
                    if (mItemClickListen != null) {
                        mItemClickListen.onItemClick(v, getAdapterPosition());
                    }
                });
            }
        }
    }


}
